package com.fbli.test.util;

import com.fbli.test.mapper.TaskMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class IdUtil {
    @Autowired
    TaskMapper taskMapper;
    /**
     * 生成任务id
     * @return
     */
    public  String createTaskId(){
        String head="T";
        String taskId;
        String datetime = new SimpleDateFormat("yyyyMMdd").format(new Date());
        //当前日期初始id
        taskId=head+datetime+"0001";
        //获取最大id对应的任务id
        String maxTaskId = taskMapper.selectMaxTastIdByMaxId();
        if (maxTaskId!=null&&maxTaskId.length()>0){
            //获取这个任务id的年月日信息
            String data = maxTaskId.substring(1, 9);
            //判断这个任务id是否是新一天生成的
            if(Long.parseLong(data)<Long.parseLong(datetime)){
                //这个任务id是新一天生成的
                return taskId;
            }else {
                String tail = maxTaskId.substring(1, 13);
                tail=Long.toString(Long.parseLong(tail)+1);
                taskId=head+tail;
                return taskId;
            }
        }
        return taskId;
    }
}
